set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_EventComment_Create]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@n4EventSN	int	
,	@strComment	nvarchar(4000)	
,	@n4UserNo	int	
,	@strUserID	varchar(50)	
,	@strUserName	nvarchar(50)
,	@strUserIP	varchar(50)
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

DECLARE	@n4UniqueUserCount INT
DECLARE @dtEventStart DATETIME
DECLARE @dtEventEnd DATETIME
-- Implementation

SET @n4UniqueUserCount = 0

SELECT
	@dtEventStart = dtEventStart
,	@dtEventEnd = dtEventEnd
FROM
	dbo.TB_Event with ( nolock )
WHERE
	n4EventSN = @n4EventSN

INSERT	TB_EventComment
(
	n4EventSN
,	strComment
,	n4UserNo
,	strUserID
,	strUserName
,	dtCreateDate
,	strUserIP
,	IsRemove
)
SELECT
	@n4EventSN
,	@strComment
,	@n4UserNo
,	@strUserID
,	@strUserName
,	@FrkDateNow
,	@strUserIP
,	0
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount


IF ( @dtEventStart <= @FrkDateNow AND @dtEventEnd >= @FrkDateNow )
BEGIN
	UPDATE	
		TB_EventCommentWriter
	SET
		n4Count = n4Count + 1
	,	dtModifyDate = @FrkDateNow
	WHERE
		n4EventSN = @n4EventSN
	AND	n4UserNo = @n4UserNo
	SELECT
		@FrkErrorCode = @@ERROR
	,	@FrkRowCount = @@RowCount
	IF ( @FrkRowCount = 0 )
	BEGIN
		SET @n4UniqueUserCount = 1
		INSERT	dbo.TB_EventCommentWriter
		(
			n4EventSN
		,	n4UserNo
		,	n4Count
		,	dtCreateDate
		,	dtModifyDate
		)
		SELECT
			@n4EventSN
		,	@n4UserNo
		,	0
		,	@FrkDateNow
		,	@FrkDateNow
	END

	UPDATE
		TB_Event
	SET
		n4TotalCommentCount = n4TotalCommentCount + 1
	,	n4ParticipationCount = n4ParticipationCount + 1
	,	n4UniqueUserCount = n4UniqueUserCount + @n4UniqueUserCount
	WHERE
		n4EventSN = @n4EventSN
END
ELSE
BEGIN
	UPDATE
		TB_Event
	SET
		n4TotalCommentCount = n4TotalCommentCount + 1
	WHERE
		n4EventSN = @n4EventSN
END

-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
